package org.wlw.teacher.servlet;

import org.wlw.teacher.anno.Resource;
import org.wlw.teacher.models.Department;
import org.wlw.teacher.models.Pager;
import org.wlw.teacher.models.Teacher;
import org.wlw.teacher.service.IDepartmentService;
import org.wlw.teacher.service.ITeacherService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/admin/teacher")
public class TeacherServlet extends BaseServlet{

    private ITeacherService teacherService;
    private IDepartmentService departmentService;

    @Resource
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Resource
    public void setTeacherService(ITeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        int depId = 0;
        try {
            depId = Integer.parseInt(request.getParameter("depId"));
        } catch (Exception e) {
        }
        Teacher t = new Teacher();
        t.setName(name);
        t.setMobile(mobile);
        t.setDepId(depId);
        Pager<Teacher> teachers = teacherService.find(t);
        List<Department> departments = departmentService.list(null);
        request.setAttribute("datas",teachers);
        request.setAttribute("searchT",t);
        request.setAttribute("deps",departments);
        return "teacher/list";
    }

    public String add(HttpServletRequest request,HttpServletResponse response) {
        List<Department> deps = departmentService.list(null);
        request.setAttribute("deps",deps);
        return "teacher/add";
    }

    public String addAction(HttpServletRequest request,HttpServletResponse response) {
        Teacher teacher = new Teacher();
        teacher.setName(request.getParameter("name"));
        teacher.setMobile(request.getParameter("mobile"));
        try {
            teacher.setSalary(Double.parseDouble(request.getParameter("salary")));
        } catch (NumberFormatException e) {
            teacher.setSalary(0);
        }
        teacher.setTno(request.getParameter("tno"));
        teacher.setDepId(Integer.parseInt(request.getParameter("depId")));
        teacherService.add(teacher);
        return getRedirectUrl("/admin/teacher");
    }

    public String delete(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        teacherService.delete(id);
        return getRedirectUrl("/admin/teacher");
    }

    public String update(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        request.setAttribute("teacher",teacherService.load(id));
        request.setAttribute("deps",departmentService.find(null));
        return "teacher/update";
    }

    public String updateAction(HttpServletRequest request,HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Teacher teacher = teacherService.load(id);
        teacher.setDepId(Integer.parseInt(request.getParameter("depId")));
        try {
            teacher.setSalary(Double.parseDouble(request.getParameter("salary")));
        } catch (NumberFormatException e) {
            teacher.setSalary(0);
        }
        teacher.setName(request.getParameter("name"));
        teacher.setMobile(request.getParameter("mobile"));
        teacherService.update(teacher);
        return getRedirectUrl("/admin/teacher");
    }
}
